<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class News extends Backend
{
    
    /**
     * Newscate模型对象
     * @var \app\admin\model\Newscate
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Newscate;

    }
    //添加新闻分类
    public function addnewscate()
    {
        if($this -> request -> isPost()){
            $input = input('post.');
            $input['uid'] = session('userinfo:id');
            $input['create_time'] = time();
            $input['update_time'] = time();
            $insertResult = model('newscate') -> isUpdate(false) -> save($input);
            if($insertResult){
                return json(['status' => 1, 'info' => '添加成功！']);
            }
            return json(['status' => 0, 'info' => '添加失败！']);
        }
        return view();
    }
    //添加新闻
    public function addnews()
    {
        if($this -> request -> isPost()){
            $input = input('post.');
            // 过滤javascript与CSS
            $input['content'] = preg_replace('/<script[^>]*?>.*?<\/script>/si','',$input['content']);

            if(session('userinfo:version') == 2) {
                $input['content'] = htmlspecialchars_decode($input['content']);
                $input['content'] = preg_replace("/<a[^>]*>(.*?)<.*?\/a.*?>/is", "$1", $input['content']);
            }
            $input['create_time'] = time();
            $input['uid'] = session('userinfo:id');
            if(session('userinfo:classify_remark')) {
                $input['classify_remark'] = session('userinfo:classify_remark');
            }
            $result = model('news') -> insert($input);
            if($result){
                return json(['status' => 1, 'info' => '添加成功！']);
            }else{
                return json(['status' => 0, 'info' => '添加失败！']);
            }
        }

        $newsCate = model('newscate') -> where(['uid' => session('userinfo:id')]) -> select();
        $this -> assign('newsCate', $newsCate);
        return view();
    }
    //新闻搜索
    public function searchNewsKeywords(){
        $search = input('post.');
        if(!empty($search['search'])){
            session('keywords:search', $search['search']);
        }else{
            session('keywords:search', null);
        }
    }
    //新闻列表
    public function newslist()
    {
        $keySearch = session('keywords:search');
        if(!empty($keySearch)) {
            $searchKeyword['title'] = ['like', '%'.$keySearch .'%'];
            $searchKeyword['keywords'] = ['like', '%'.$keySearch .'%'];
            $searchKeyword['detail'] = ['like', '%'.$keySearch .'%'];
        }else{
            $searchKeyword = [];
        }

        $newsList = model('news')
            -> where(['uid' => session('userinfo:id')])
            ->where(function ($q) use($searchKeyword) {
                $q->whereOr($searchKeyword);
            })
            -> order('id desc')
            -> paginate(5);
        $page = $newsList -> render();

        foreach ($newsList as &$val){
            $val['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
        }

        $this -> assign('page', $page);
        $this -> assign('newsList', $newsList);

        return view();
    }
    //新闻分类搜索
    public function searchNewscateKeywords(){
        $search = input('post.');
        if(!empty($search['search'])){
            session('newscate:keywords:search', $search['search']);
        }else{
            session('newscate:keywords:search', null);
        }
    }
    //分类列表
    public function categorylist(){

        $keySearch = session('newscate:keywords:search');
        if(!empty($keySearch)) {
            $searchKeyword['catename'] = ['like', '%'.$keySearch .'%'];
        }else{
            $searchKeyword = [];
        }

        $cateResult = model('newscate')
            -> where(['uid' => session('userinfo:id')])
            -> where($searchKeyword)
            -> order('id desc')
            -> paginate(5);
        $page = $cateResult -> render();

        $this -> assign('page', $page);
        $this -> assign('newscate', $cateResult);

        return view();
    }
    //编辑分类
    public function editnewscate(){
        if($this -> request -> isPost()){
            $input = input('post.');
            $input['update_time'] = time();
            $result = model('newscate') -> where(['id' => $input['id']]) -> update($input);
            if($result){
                return json(['status' => 1, 'info' => '更新成功！']);
            }else{
                return json(['status' => 0, 'info' => '更新失败！']);
            }
        }

        if($this -> request -> isGet()){
            $input['id'] = input('param.id');
            $result = model('newscate') -> field('id,catename') -> where(['id' => $input['id']]) -> find();
            if($result) {
                return json(['status' => 1, 'data' => $result, 'info' => '获取成功！']);
            }else{
                return json(['status' => 0, 'info' => '获取信息失败！']);
            }
        }
    }
    //删除新闻分类
    public function delcate(){
        $id = input('id');
        $delResult = model('newscate') -> where(['id' => $id]) -> delete();
        if($delResult) {
            echo "<script>top.location.href='/admin/news/categorylist?ref=addtabs'</script>";
        }else{
            echo "<script>alert('删除失败！');top.location.href='/admin/news/categorylist?ref=addtabs'</script>";
        }
    }
    //编辑新闻
    public function editnews(){
        if($this -> request -> isPost()){
            $input = input('post.');
            // 过滤javascript与CSS
            $input['content'] = preg_replace('/<script[^>]*?>.*?<\/script>/si','',$input['content']);
            if(session('userinfo:version') == 2) {
                $input['content'] = htmlspecialchars_decode($input['content']);
                $input['content'] = preg_replace("/<a[^>]*>(.*?)<.*?\/a.*?>/is", "$1", $input['content']);
            }
            $updateResult = model('news') -> update($input);
            if($updateResult){
                return json(['status' => 1, 'info' => '更新成功！']);
            }else{
                return json(['status' => 0, 'info' => '更新失败！']);
            }
        }

        $id = input('id');
        $newsCate = model('newscate') -> where(['uid' => session('userinfo:id')]) -> select();
        $onenews = model('news') -> where(['id' => $id]) -> find();

        $this -> assign('newsCate', $newsCate);
        $this -> assign('onenews', $onenews);
        return view();
    }
    //删除新闻
    public function delnews(){
        $id = input('id');
        $delResult = model('news') -> where(['id' => $id]) -> delete();
        if($delResult) {
            echo "<script>top.location.href='/admin/news/newslist?ref=addtabs'</script>";
        }else{
            echo "<script>alert('删除失败！');top.location.href='/admin/news/newslist?ref=addtabs'</script>";
        }
    }

    //批量删除新闻分类
    public function delSelectInput(){
        $input = input('post.');
        $inputid = json_decode($input['checkList'], true);
        $ids['id'] = implode(',', $inputid);
        $delResult = model('newscate') -> where(['id' => ['in', $ids['id']]]) -> delete();
        if($delResult){
            return json(['status' => 1, 'info' => '删除成功！']);
        }
        return json(['status' => 0, 'info' => '删除失败！']);
    }
    //批量删除新闻
    public function delSelectInputNews(){
        $input = input('post.');
        $inputid = json_decode($input['checkList'], true);
        $ids['id'] = implode(',', $inputid);
        $delResult = model('news') -> where(['id' => ['in', $ids['id']]]) -> delete();
        if($delResult){
            return json(['status' => 1, 'info' => '删除成功！']);
        }
        return json(['status' => 0, 'info' => '删除失败！']);
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

}
